package com.net.core.config;

import com.net.core.models.common.CommonService;
import com.net.core.utils.MyUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.el.BeanNameResolver;
import javax.sql.DataSource;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.sql.Connection;
import java.sql.SQLException;

@Slf4j
@Component
@Order(-1000)
public class AppContextUtils implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        AppContextUtils.applicationContext = applicationContext;
    }

    public static <T> T getBean(Class<T> clazz) {
        return applicationContext.getBean(clazz);
    }

    static String serverPort;
    static String serverName;

    public static String getHttpBaseUrl() throws UnknownHostException {
        String ip = InetAddress.getLocalHost().getHostAddress();
        return "http://" + ip + ":" + serverPort + serverName;
    }

    @Resource
    CommonService commonService;

    public static void initInfo() throws UnknownHostException, SQLException {
        Environment env = applicationContext.getEnvironment();
        String ip = InetAddress.getLocalHost().getHostAddress();
        serverPort = env.getProperty("server.port");
        if (serverPort == null) serverPort = "8080";
        String path = env.getProperty("server.servlet.context-path");
        String name = env.getProperty("spring.application.name");
        name = StringUtils.isEmpty(name) ? "Application" : name;
        serverName = StringUtils.isEmpty(path) ? "" : path;
        Connection connection = applicationContext.getBean(DataSource.class).getConnection();
        String url = connection.getMetaData().getURL();
        log.info("\n----------------------------------------------------------\n\t"
                + name + " is running! Access URLs:\n"
                + "\t数据库地址: " + url + "\n"
                + "\t本地访问地址: \thttp://localhost:" + serverPort + serverName + "/doc.html\n"
                + "\t本地访问地址: \thttp://localhost:" + serverPort + serverName + "/admin/index.html\n"
                + "\t本地访问地址: \thttp://" + ip + ":" + serverPort + serverName + "/admin/index.html\n"
                + "\t接口文档地址: \thttp://" + ip + ":" + serverPort + serverName + "/doc.html\n"
                + "\t接口数据监听: \thttp://localhost:" + serverPort + serverName + "/druid\n"
                + "\n---------------------启动完成---------------------");
        //API 文档登录账号密码 配置在对应数据库的YXNET库的interface_user 表  密码由MD5加密，无法解密，只能重置  。
        // 账号 admin  密码 123
//        checkTable();
    }


}
